/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cms.action.adminaction;

import cms.sessionbean.UserPortfoiloRemote;
import cms.util.EjbLocator;
import com.opensymphony.xwork2.ActionSupport;
import dto.Conference;
import dto.Profile;
import dto.User;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Timmy
 */
public class UserMgmtAction  extends ActionSupport{
    
     private List<Conference> conferences;
     private List<User> users; 
     private List<Profile> userprofiles; 
     private String userName; 
     private User oneAdmin;
     private String passWord;
     private String confirmpassword;
     private Integer IsUpdate ; 

    public Integer getIsUpdate() {
        return IsUpdate;
    }

    public void setIsUpdate(Integer IsUpdate) {
        this.IsUpdate = IsUpdate;
    }

 

    public String getConfirmpassword() {
        return confirmpassword;
    }

    public void setConfirmpassword(String confirmpassword) {
        this.confirmpassword = confirmpassword;
    }

    public String getPassWord() {
        return passWord;
    }

    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public User getOneAdmin() {
        return oneAdmin;
    }

    public void setOneAdmin(User oneAdmin) {
        this.oneAdmin = oneAdmin;
    }

    public Profile getOneProfile() {
        return oneProfile;
    }

    public void setOneProfile(Profile oneProfile) {
        this.oneProfile = oneProfile;
    }
     private Profile oneProfile; 

    public List<Profile> getUserprofiles() {
        return userprofiles;
    }

    public void setUserprofiles(List<Profile> userprofiles) {
        this.userprofiles = userprofiles;
    }

    public List<User> getUsers() {
        return users;
    }

    public void setUsers(List<User> users) {
        this.users = users;
    }
     
    public List<Conference> getConferences() {
        return conferences;
    }

    public void setConferences(List<Conference> conferences) {
        this.conferences = conferences;
    }
    private String redirectURL; 

    public String getRedirectURL() {
        return redirectURL;
    }

    public void setRedirectURL(String redirectURL) {
        this.redirectURL = redirectURL;
    }
    
    public UserMgmtAction() {
    }
    
    public String execute() throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    public String register()
    {
        this.oneProfile = null; 
        this.IsUpdate = 0 ;
        return "register";
    }
    
    public String processUpdate()
    {
        if (!this.passWord.equals(this.confirmpassword))
        {
            this.addFieldError("password", "Invalid password");
            return "register";
        }
        
        if ( this.userName.length()== 0)
        {
            this.addFieldError("userName", "Invalid Username");
            return "register";
        }
        
         UserPortfoiloRemote cb = (UserPortfoiloRemote)EjbLocator.getInstance().LookUpBeanByName(UserPortfoiloRemote.class, "UserPortfoilo");
         User oneUser = cb.getUser(this.userName);
         if (oneUser != null ) // modify
         {
             if (this.IsUpdate == 0)
             {
                 // wrong ,already exists
                 this.addFieldError("userName", "Username already exists");
                 return "register";
             }
             
             if (this.passWord.length() != 0) // update password
             {
                 String md = org.apache.commons.codec.digest.DigestUtils.md5Hex(this.passWord);
                 oneUser.setPassword(md);
             }
         }
         else 
         {
             if (this.passWord.length() == 0)
             {
                    this.addFieldError("password", "Password can not be null");
                    return "register";
             }
             
             oneUser = new User();
             oneUser.setUserName(userName);
             oneUser.setRole("member");
             String md = org.apache.commons.codec.digest.DigestUtils.md5Hex(this.passWord);
             oneUser.setPassword(md);
         }
         
         if(this.oneProfile == null )
         {
             this.oneProfile = new Profile();
             this.oneProfile.setUserName(userName);
         }
         
         cb.updateUser(oneUser);
         cb.updateUserProfile(oneProfile);
        
        this.redirectURL = "listall";
        return "updatesuccess";
    }

    public String processAdminRegister()
    {
        if (!oneAdmin.getPassword().equals(this.confirmpassword) || this.confirmpassword.equals(""))
        {
            this.addFieldError("password", "Password is not equal");
            return "registerAdmin";
        }
        oneAdmin.setPassword(org.apache.commons.codec.digest.DigestUtils.md5Hex(oneAdmin.getPassword()));
        oneAdmin.setRole("admin");
         UserPortfoiloRemote cb = (UserPortfoiloRemote)EjbLocator.getInstance().LookUpBeanByName(UserPortfoiloRemote.class, "UserPortfoilo");
         cb.updateUser(oneAdmin);
        
         
        this.redirectURL = "adminlist";
        return "updatesuccess";
    }
    public String listall()
    {
         UserPortfoiloRemote cb = (UserPortfoiloRemote)EjbLocator.getInstance().LookUpBeanByName(UserPortfoiloRemote.class, "UserPortfoilo");
         this.users = cb.getAllUsers("member");
         this.userprofiles = new ArrayList<Profile>();
         for (User user :this.users)
         {
             Profile profile =  cb.getUserProfile(user.getUserName());
             if (profile == null )
             {
                 profile = new Profile();
                 profile.setUserName(user.getUserName());
             }
             this.userprofiles.add(profile);
         }
         return "list";
    }
    
    public String RegisterAdmin()
    {
        return "registerAdmin";
    }
    
   public String adminlist()
   {
       UserPortfoiloRemote cb = (UserPortfoiloRemote)EjbLocator.getInstance().LookUpBeanByName(UserPortfoiloRemote.class, "UserPortfoilo");
       this.users = cb.getAllUsers("admin");
       return "adminlist";
   }
   
   public String editadmin()
   {
       return "editadmin";
   }
   
   public String editUser()
   {
       UserPortfoiloRemote cb = (UserPortfoiloRemote)EjbLocator.getInstance().LookUpBeanByName(UserPortfoiloRemote.class, "UserPortfoilo");
       this.IsUpdate = 1; 
       this.oneProfile = cb.getUserProfile(userName);
       if (this.oneProfile == null)
       {
           this.oneProfile = new Profile();
           this.oneProfile.setUserName(userName);
       }
       return "edituser";
   }
   
   public String processChangeAdminsPsw()
   {
       UserPortfoiloRemote cb = (UserPortfoiloRemote)EjbLocator.getInstance().LookUpBeanByName(UserPortfoiloRemote.class, "UserPortfoilo");
       this.oneAdmin = cb.getUser(userName);
       String md = org.apache.commons.codec.digest.DigestUtils.md5Hex(this.passWord);
       oneAdmin.setPassword(md);
       cb.updateUser(oneAdmin);
       
       this.redirectURL = "adminlist";
       return "updatesuccess";
   }
   
}
